package com.gzhryc.net_marketing.cloud.micro.api;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

import org.apache.log4j.xml.DOMConfigurator;

import com.gzhryc.common.config.PropertyTools;
import com.gzhryc.common.event.DelayHandleService;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.common.redis.RedisTools;
import com.gzhryc.jdbc.ConnectionFactory;
import com.gzhryc.jdbc.models.ConnectInfo;
import com.gzhryc.net_marketing.cloud.MarketConstants;
import com.gzhryc.net_marketing.cloud.account.HttpCallbackDelayHandler;
import com.gzhryc.net_marketing.cloud.business.event.PayOrderLogEventHandler;
import com.gzhryc.net_marketing.cloud.micro.api.jetty.JettyHttpServer;
import com.gzhryc.net_marketing.cloud.notice.SupplyChainAPIService;
import com.gzhryc.shared_device.common.BusyThreadPool;
import com.gzhryc.shared_device.common.CountryAreaUtils;
import com.gzhryc.shared_device.oem.code.BaseConstants;
import com.gzhryc.shared_device.oem.code.PayServiceFactory;

public class Main {

	static Logger log = Logger.getLogger(Main.class);

	public static void main(String[] args) {
		try {
			String path = "config";
			if (args != null && args.length > 0) {
				path = args[0];
			}

			// 获取当前程序路径
			File logCfgFile = new File(path + "/log4j.xml");
			if (logCfgFile.exists()) {
				DOMConfigurator.configure(logCfgFile.getPath());
				log.info("加载" + path + "/log4j.xml日志配置信息");
			} else {
				DOMConfigurator.configure(Main.class.getClassLoader().getResource("log4j.xml"));
				log.info("加载默认log4j.xml日志配置信息");
			}

			File infoCfgFile = new File(path + "/info.properties");
			if (infoCfgFile.exists()) {
				PropertyTools.load(infoCfgFile);
				log.info("加载" + path + "/info.properties配置信息");
			} else {
				PropertyTools.load(Main.class.getClassLoader().getResourceAsStream("info.properties"));
				log.info("加载默认info.properties配置信息");
			}
			
			File redisCfgFile = new File(path + "/redis.properties");
			if (redisCfgFile.exists()) {
				PropertyTools tools = new PropertyTools(redisCfgFile);
				RedisTools.get().initial(tools.getProperties());
				log.info("加载" + path + "/redis.properties配置信息");
			} else {
				PropertyTools tools = new PropertyTools(Main.class.getClassLoader().getResourceAsStream("redis.properties"));
				RedisTools.get().initial(tools.getProperties());
				log.info("加载默认redis.properties配置信息");
			}
			
			File jdbcCfgFile = new File(path + "/jdbc.properties");
			if (jdbcCfgFile.exists()) {
				Properties properties = new Properties();
				InputStream inStream = new FileInputStream(jdbcCfgFile);
				BufferedReader bf = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
				properties.load(bf);
				ConnectInfo connectInfoCfg = new ConnectInfo(properties);
				if(MicroConstants.isDebug() >= 1){
					connectInfoCfg.setLogLevel(2);
				}
				ConnectionFactory.addDataSource(BaseConstants.JDBC_KEY, connectInfoCfg);
				log.info("加载" + path + "/jdbc.properties数据库连接配置信息");
			} else {
				Properties properties = new Properties();
				properties.load(Main.class.getClassLoader().getResourceAsStream("jdbc.properties"));
				ConnectInfo connectInfoCfg = new ConnectInfo(properties);
				connectInfoCfg.setLogLevel(2);
				ConnectionFactory.addDataSource(BaseConstants.JDBC_KEY, connectInfoCfg);
				log.info("加载默认jdbc.properties数据库连接配置信息");
			}

			ConnectInfo info = null;
			File marketJdbcCfgFile = new File(path + "/jdbc_market.properties");
			if (marketJdbcCfgFile.exists()) {
				Properties properties = new Properties();
				InputStream inStream = new FileInputStream(marketJdbcCfgFile);
				BufferedReader bf = new BufferedReader(new InputStreamReader(inStream, "UTF-8"));
				properties.load(bf);
				info = new ConnectInfo(properties);
				if(MicroConstants.isDebug() >= 1) {
					info.setLogLevel(2);
				}
				ConnectionFactory.addDataSource(MarketConstants.JDBC_KEY, info);
				ConnectionFactory.setDefaultJdbcKey(MarketConstants.JDBC_KEY);
				log.info("加载" + path + "/jdbc_market.properties数据库连接配置信息");
			} else {
				Properties properties = new Properties();
				properties.load(Main.class.getClassLoader().getResourceAsStream("jdbc_market.properties"));
				info = new ConnectInfo(properties);
				info.setLogLevel(2);
				ConnectionFactory.addDataSource(MarketConstants.JDBC_KEY, info);
				ConnectionFactory.setDefaultJdbcKey(MarketConstants.JDBC_KEY);
				log.info("加载默认jdbc_market.properties数据库连接配置信息");
			}
			
			//初始化工作线程
			BusyThreadPool.self().initial();

			//初始化地区
			CountryAreaUtils.initial();
			
			//初始化支付
			PayServiceFactory.self().initial(PropertyTools.get().getString("httpDomain"));
			
			String marketCloudHttpDomain = PropertyTools.get().getString("marketSupplierHttpDomain");
			String marketCloudToken = PropertyTools.get().getString("marketSupplierToken");
			SupplyChainAPIService.self().initial(marketCloudHttpDomain,marketCloudToken);

			DelayHandleService.get().run(7);
			DelayHandleService.get().addHandler(new HttpCallbackDelayHandler());		//回调延迟处理
			DelayHandleService.get().addHandler(new PayOrderLogEventHandler());

			JettyHttpServer.start();

		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
